//package com.example.crud.poi_excel_photo;
//
//import java.awt.image.BufferedImage;
//import java.io.ByteArrayInputStream;
//import java.io.File;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//import javax.imageio.ImageIO;
//import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
//import org.apache.poi.ss.usermodel.Cell;
//import org.apache.poi.ss.usermodel.ClientAnchor;
//import org.apache.poi.ss.usermodel.Drawing;
//import org.apache.poi.ss.usermodel.Picture;
//import org.apache.poi.ss.usermodel.Row;
//import org.apache.poi.ss.usermodel.Sheet;
//import org.apache.poi.ss.usermodel.Workbook;
//import org.apache.poi.ss.usermodel.WorkbookFactory;
//import org.apache.poi.util.IOUtils;
//
//public class ExcelImageReader {
//    public static List<BufferedImage> readExcelWithImages(File excelFile) throws InvalidFormatException, IOException {
//        List<BufferedImage> images = new ArrayList<>();
//        Workbook workbook = WorkbookFactory.create(excelFile);
//
//        for (Sheet sheet : workbook) {
//            for (Row row : sheet) {
//                for (Cell cell : row) {
//                    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA && cell.getCachedFormulaResultType() == Cell.CELL_TYPE_ERROR) {
//                        // If the cell contains a formula that resulted in an error, skip it
//                        continue;
//                    }
//
//                    int pictureIndex;
//                    Drawing<?> drawing;
//                    ClientAnchor anchor;
//
//                    switch (cell.getCellType()) {
//                        case Cell.CELL_TYPE_BLANK:
//                        case Cell.CELL_TYPE_STRING:
//                        case Cell.CELL_TYPE_BOOLEAN:
//                            // These cell types don't contain images
//                            continue;
//
//                        case Cell.CELL_TYPE_NUMERIC:
//                            if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
//                                // Dates don't contain images
//                                continue;
//                            }
//                            break;
//
//                        case Cell.CELL_TYPE_FORMULA:
//                            switch (cell.getCachedFormulaResultType()) {
//                                case Cell.CELL_TYPE_BLANK:
//                                case Cell.CELL_TYPE_STRING:
//                                case Cell.CELL_TYPE_BOOLEAN:
//                                    // These formula result types don't contain images
//                                    continue;
//
//                                case Cell.CELL_TYPE_NUMERIC:
//                                    if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
//                                        // Dates don't contain images
//                                        continue;
//                                    }
//                                    break;
//
//                                default:
//                                    // Ignore other formula result types
//                                    continue;
//                            }
//                            break;
//
//                        default:
//                            // Ignore other cell types
//                            continue;
//                    }
//
//                    // Get the picture index from the cell if possible
//                    try {
//                        pictureIndex = cell.getCellStyle().getFillForegroundColor(); // this is actually the picture index
//                    } catch (Exception e) {
//                        // This cell is not associated with a picture
//                        continue;
//                    }
//
//                    // Get the drawing object for this sheet
//                    drawing = sheet.getDrawingPatriarch();
//                    if (drawing == null) {
//                        // This sheet doesn't contain any pictures
//                        continue;
//                    }
//
//                    // Get the anchor for this cell's picture
//                    for (Object obj : drawing.getChildren()) {
//                        if (obj instanceof Picture) {
//                            Picture picture = (Picture) obj;
//                            if (picture.getPictureIndex() == pictureIndex) {
//                                anchor = (ClientAnchor) picture.getAnchor();
//                                break;
//                            }
//                        }
//                    }
//
//                    if (anchor == null) {
//                        // This cell's picture couldn't be found
//                        continue;
//                    }
//
//                    // Get the picture image data
//                    byte[] pictureData = null;
//                    for (Object obj : drawing.getCTDrawing().getBlipFillOrBlip()) {
//                        if (obj instanceof org.openxmlformats.schemas.drawingml.x2006.findAnnoMao.CTBlip) {
//                            org.openxmlformats.schemas.drawingml.x2006.findAnnoMao.CTBlip blip = (org.openxmlformats.schemas.drawingml.x2006.findAnnoMao.CTBlip) obj;
//                            if (blip.getEmbed() != null) {
//                                int imagePartIndex = Integer.parseInt(blip.getEmbed());
//                                org.apache.poi.openxml4j.opc.PackagePart imagePart = workbook.getPackage().getPartById("rId" + imagePartIndex);
//                                pictureData = IOUtils.toByteArray(imagePart.getInputStream());
//                                break;
//                            }
//                        }
//                    }
//
//                    if (pictureData == null) {
//                        // This picture's data couldn't be found
//                        continue;
//                    }
//
//                    // Convert the picture image data to a BufferedImage
//                    BufferedImage pictureImage = ImageIO.read(new ByteArrayInputStream(pictureData));
//
//                    // Add the pictureImage to the list
//                    images.add(pictureImage);
//                }
//            }
//        }
//
//        workbook.close();
//
//        return images;
//    }
//}
